<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="component.crossdomain.CrossDomainConstant"%>

<%

	String JSESSIONID = request.getParameter("JSESSIONID");

	System.out.println("domainFrom传过来的JSESSIONID = " + JSESSIONID);
	
	Cookie[] cookieArr = request.getCookies();
	System.out.println("cookieArr = " + cookieArr);
	if(cookieArr != null) {
		for(int i = 0;i<cookieArr.length;i++) {
			String name = cookieArr[i].getName();
			String value = cookieArr[i].getValue();
			System.out.println("name = " + value);
			if("JSESSIONID".equals(name)) {
				System.out.println("cookied的JSESSIONID = " + value);
			}
		}
	}
	
	JSESSIONID = request.getSession().getId();//获取当前JSESSIONID （不管是从主域还是二级域访问产生）
	System.out.println("domainTo的JSESSIONID = " + JSESSIONID);
	
	
	Cookie cookie = new Cookie("JSESSIONID", JSESSIONID);
	cookie.setDomain("localhost"); //关键在这里，将cookie设成主域名访问，确保不同域之间都能获取到该cookie的值，从而确保session统一
	cookie.setPath(CrossDomainConstant.DOMAIN_FROM);
	response.addCookie(cookie); //将cookie返回到客户端

	

	String userId = (String)session.getAttribute("userId");
	if(userId == null) {
		System.out.println("第一次访问");
		session.setAttribute("userId","test");
	} else {
		System.out.println("第n次访问userId = " + userId);
	}
%>

JSESSIONID：<input type="text" id="JSESSIONID" name="JSESSIONID" value="<%=JSESSIONID%>" size="50"/> <br>
userId：&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<input type="text" value="<%=userId%>" />